package com.ms.p2p.service.impl;

import com.ms.common.exception.NoticeException;
import com.ms.common.utils.AppContext;
import com.ms.common.utils.MsDateUtils;
import com.ms.p2p.Constants;
import com.ms.p2p.dao.MemberAccountRechargeDao;
import com.ms.p2p.entity.account.AccountRecharge;
import com.ms.p2p.enums.AccountFLow;
import com.ms.p2p.enums.AccountType;
import com.ms.p2p.provider.MemberAccountProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true)
public class MemberAccountRechargeServiceImpl  {
	@Autowired
	private MemberAccountRechargeDao memberAccountRechargeDao;
	@Autowired
	private MemberAccountServiceImpl memberAccountService;


;


	/**
	 * 充值记录
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int insert(AccountRecharge recharge) {
		return memberAccountRechargeDao.insert(recharge);

	}

	/**
	 * 根基ID查充值记录
	 *
	 * @param rechagreId
	 * @return
	 */
	public AccountRecharge findById(int rechagreId) {
		return memberAccountRechargeDao.getByIdStatus(rechagreId,
				null);

	}

	public AccountRecharge findByIdStatus0(int rechagreId) {
		return (AccountRecharge) memberAccountRechargeDao
				.getByIdStatus(rechagreId, 0);
	}

	public Map<String, Object> findDetail(int rechagreId) {
		return memberAccountRechargeDao.findDetail(rechagreId);

	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public int updateVerify(AccountRecharge map) {
		return memberAccountRechargeDao.updateByMapAndStatus(map);
	}


	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void onlineRecharge(String ip,AccountRecharge recharge, String name) {
		memberAccountService.OperAccount(ip,recharge.getMoney(),recharge.getUser_id(),name, AccountType.recharge.getValue(), AccountFLow.FLOW_IN.getValue());
		recharge.setStatus(1);
		recharge.setVerify_userid(1);
		recharge.setVerify_time(MsDateUtils.getTime());
		recharge.setVerify_remark("网银支付");
		if(updateVerify(recharge)==0){
			throw new NoticeException("已回调成功一次");
		}


	}

	public String selectTotalTakeCash(int user_id) {
		return memberAccountRechargeDao.findTotalTakeCash(user_id);

	}

	public List<AccountRecharge> findPager(Integer user_id, int begin,
										   Integer pageSize) {

		return memberAccountRechargeDao.findPagerF(user_id + "", begin, pageSize);
	}

	public int findCount(Integer user_id) {
		// TODO Auto-generated method stub
		return memberAccountRechargeDao.findCountF(user_id + "");
	}

	public List<Map<String, Object>> findPaager(String rechargeType,String payWay,
												String money, String userName, String trade_no, String status,
												String begin_date, String end_date, int begin, Integer pageSize) {
		return memberAccountRechargeDao.findPagerM(rechargeType,payWay, money, userName,
				trade_no, status, begin_date, end_date, begin, pageSize);
	}

	public int findCount(String rechargeType,String payWay, String money, String userName,
						 String trade_no, String status, String begin_date, String end_date) {
		return memberAccountRechargeDao.findCountM(rechargeType,payWay, money, userName,
				trade_no, status, begin_date, end_date);
	}


	public String selectTotalRecharge(Integer id) {
		return memberAccountRechargeDao.findTotalRecharge(id);
	}

	public AccountRecharge findByTrano(String requestId, Integer status) {
		return memberAccountRechargeDao.findByTrano(requestId, status);


	}


}
